package com.itheima.shop.web.servlet;

import com.google.gson.Gson;
import com.itheima.shop.entity.Category;
import com.itheima.shop.entity.PageBean;
import com.itheima.shop.entity.Product;
import com.itheima.shop.service.CategoryService;
import com.itheima.shop.service.ProductService;
import com.itheima.shop.util.JedisUtil;
import com.itheima.shop.util.JsonUtil;
import redis.clients.jedis.Jedis;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "CategoryServlet", value = "/CategoryServlet")
public class CategoryServlet extends BaseServlet {
    /**
     * 查询所有类别,分页查询
     */
    protected void findAllCategory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            int currPageNO = Integer.parseInt(request.getParameter("page"));
            int perPageNO = Integer.parseInt(request.getParameter("rows"));

            CategoryService service = new CategoryService();
            PageBean<Category> pageBean = service.findAllCategory(currPageNO, perPageNO);
            Map<String, Object> json = new HashMap<>();
            json.put("total", pageBean.getAllRecordNO());
            json.put("rows", pageBean.getList());
            JsonUtil.writeJson(response, json);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("message", "商品查询失败!");
            request.getRequestDispatcher("/WEB-INF/message.jsp").forward(request, response);
        }
    }
    /**
     * 修改类别
     */
    private void updateCategory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            int cid = Integer.parseInt(request.getParameter("cid"));
            String cname = request.getParameter("cname");
            CategoryService service = new CategoryService();

            if (service.updateCategory(cid, cname)) {
                String result = "{\"success\":true}";
                JsonUtil.writeString(response, result);
            } else {
                String result = "{\"errorMsg\":出错了}";
                JsonUtil.writeString(response, result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除类别
     */
    private void deleteCategory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            int cid = Integer.parseInt(request.getParameter("cid"));

            CategoryService service = new CategoryService();
            if (service.deleteCategory(cid)) {
                String result = "{\"success\":true}";
                JsonUtil.writeString(response, result);
            } else {
                String result = "{\"success\":false}";
                JsonUtil.writeString(response, result);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    /**
     * 增加类别
     */
    private void addCategory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            String cname = request.getParameter("cname").trim();
            System.out.println(cname);
            CategoryService service = new CategoryService();
            if (service.addCategory(cname)) {
                String result = "{\"success\":true}";
                JsonUtil.writeString(response, result);
            } else {
                String result = "{\"errorMsg\":出错了}";
                JsonUtil.writeString(response, result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 查询所有类别,用于填充form表单
     */
    protected void findAllCategoryAjax(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            CategoryService service = new CategoryService();
            List<Category> categoryList = service.findAllCategoryAjax();
            JsonUtil.writeJson(response, categoryList);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("message", "商品查询失败!");
            request.getRequestDispatcher("/WEB-INF/message.jsp").forward(request, response);
        }
    }
    /**
     * 同步数据
     */
    protected void backToFore(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            Jedis jedis=JedisUtil.getJedis();
            jedis.del("categoryJSON");
            JedisUtil.close(jedis);
            JsonUtil.writeString(response, "true");
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("message", "同步失败!");
            request.getRequestDispatcher("/WEB-INF/message.jsp").forward(request, response);
        }
    }
}
